package com.cn.system.crawler;

import com.cn.system.crawler.annotation.PageFieldSelect;
import com.cn.system.crawler.annotation.PageSelect;
import com.cn.system.crawler.parser.PageParser;
import com.cn.system.entity.CrawlerModel;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.List;

/**
 * 爬虫示例01：爬取页面数据并封装VO对象
 *
 * @author xuxueli 2017-10-09 19:48:48
 */
public class XxlCrawlerTest {

    @PageSelect(cssQuery = ".course-wrapper--container")
    public static class PageVo {

        @PageFieldSelect(cssQuery = ".content-title")
        private String repository;

        @PageFieldSelect(cssQuery = ".content-desc")
        private String description;

        @PageFieldSelect(cssQuery = ".content-hint")
        private String count;

        @PageFieldSelect(cssQuery = ".course-wrapper--container")
        private List<Instance> instances;





        @PageSelect(cssQuery = ".course-card")
        public class Instance{

            @PageFieldSelect(cssQuery = ".course-subject")
            private String repository;

            public Instance(String repository) {
                this.repository = repository;
            }

            @Override
            public String toString() {
                return "Instance{" +
                    "repository='" + repository + '\'' +
                    '}';
            }
        }

        public List<Instance> getInstances() {
            return instances;
        }

        public void setInstances(List<Instance> instances) {
            this.instances = instances;
        }

        //        @PageFieldSelect(cssQuery = "li", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "[class:course-times]")
//        private List<String> li;


//        public List<String> getLi() {
//            return li;
//        }
//
//        public void setLi(List<String> li) {
//            this.li = li;
//        }

        public String getCount() {
            return count;
        }

        public void setCount(String count) {
            this.count = count;
        }



        public String getRepository() {
            return repository;
        }

        public void setRepository(String repository) {
            this.repository = repository;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        @Override
        public String toString() {
            return "PageVo{" +
                    "repository='" + repository + '\'' +
                    ", description='" + description + '\'' +
                ", count='" + count + '\'' +
                ", list='" + this.instances + '\'' +
                    '}';
        }
    }

    public static void mainDemo(String[] args) {

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls("https://gitee.com/xuxueli0323/projects?page=1")
               //  .setWhiteUrlRegexs("https://gitee\\.com/xuxueli0323/projects\\?page=\\d+")
                .setThreadCount(1)
                .setPageParser(new PageParser<PageVo>() {
                    @Override
                    public void parse(Document html, Element pageVoElement, PageVo pageVo) {
                        // 解析封装 PageVo 对象
                        String pageUrl = html.baseUri();
                        System.out.println(pageUrl + "：" + pageVo.toString());
                    }
                })
                .build();

        System.out.println("start");
        crawler.start(true);
        System.out.println("end");
    }


    public static void main(String[] args) {
        XxlCrawler crawler = new XxlCrawler.Builder()
            .setUrls("https://fudao.qq.com/grade/7003/")
                .setWhiteUrlRegexs("^https://fudao.qq.com/grade/*[1-9]\\d*/$")
            .setThreadCount(3)
            .setPageParser(new PageParser<CrawlerModel>() {
                @Override
                public void parse(Document html, Element pageVoElement, CrawlerModel pageVo) {
                    // 解析封装 PageVo 对象

                    // 获取数据


                    String pageUrl = html.baseUri();
                    System.out.println(pageUrl + "：" + pageVo.toString());
                }
            })
            .build();
        crawler.start(true);
    }

}
